# coding=utf-8
'''
Created on 2013-8-16

@author: ysy
'''
import datetime
import logging
import redis
from config import config
__pool = redis.ConnectionPool(host=config.REDIS_HOST, port=config.REDIS_PORT, password=config.REDIS_PWD, db=config.REDIS_DB)
__handle = redis.Redis(connection_pool=__pool)

def len_calllist(name="autocall"):
    lenth = __handle.llen(name)
    if lenth > 0:
        return True
    return False
def len_list(name="autocall"):
    lenth = __handle.llen(name)
    return lenth 

def hget_by_sn(sn):
    req = __handle.hgetall(sn)
    if req:
        return req
    return {}

def hlen(name):
    '''获取指定名称HASH的长度'''
    return __handle.hlen(name)
def hgetkeys(name):
    '''获取指定名称的keys'''
    return __handle.hkeys(name)
def hget_value(name, key):
    '''获取指定名称和key的值'''
    return __handle.hget(name, key)

def hdelete(name, key):
    '''删除指定名称的key'''
    return __handle.hdel(name, key)

def hset(name, key, value):
    '''设置指定名称的key'''
    return __handle.hset(name, key, value)


def hupdate(name, key, value):
    '''更新指定名称的key'''
    return __handle.hset(name, key, value)


def out_list(name, end=50):
    '''待通知队列取多条'''
    # return __handle.lpop("callnotify")
    return __handle.lrange(name, 0, end)

def rm_list_range(name, start=0, end=-1):
    '''待通知队列取多条'''
    # return __handle.lpop("callnotify")
    return __handle.ltrim(name, start, end)


def in_list(name, value):
    '''插入待通知队列'''
    logging.info("写入redis名称:%s,内容:%s" % (name, value))
    return __handle.rpush(name, value)

def rm_list(name, value):
    '''删除队列中value'''
    return __handle.lrem(name, value)

def del_list_key(listkey):
    return __handle.delete(listkey)


def del_key(key):
    return __handle.delete(key)


def out_calllist(name="autocall"):
    """从待呼叫队列取一个请求"""
    sn = __handle.lpop(name)
    if not sn:
        return None
    req = __handle.hgetall(sn)
    if req:
        return req
    else:
        logging.error(u"hgetall error: sn not exist %s" % sn)
    return None

def in_calllist(sn):
    '''插入待呼叫队列'''
    return __handle.rpush("autocall", sn)

def save_request(name, mapping):
    '''保存呼叫请求的数据'''
    res1 = __handle.hmset(name, mapping)
    res2 = __handle.expire(name, datetime.timedelta(minutes=30))
    return res1 and res2

def update_request(name, key, value):
    '''更新数据'''
    return __handle.hset(name, key, value)

def del_request(name):
    '''删除呼叫请求的数据'''
    return __handle.delete(name)
    
def get_request(sn):
    '''查询sn对应呼叫请求'''
    return __handle.hgetall(sn)

def out_callnotify(end=50):
    '''待通知队列取一条'''
    # return __handle.lpop("callnotify")
    return __handle.lrange("callnotify", 0, end)


def in_callnotify(url):
    '''插入待通知队列'''
    return __handle.rpush("callnotify", url)

def rm_callnotify(url):
    '''删除队列中url'''
    return __handle.lrem("callnotify", url)


def out_comebakcallnotify(end=50):
    '''待通知队列取一条'''
    # return __handle.lpop("callnotify")
    return __handle.lrange("comebakcallnotify", 0, end)


def in_comebakcallnotify(url):
    '''插入待通知队列'''
    return __handle.rpush("comebakcallnotify", url)

def rm_comebakcallnotify(url):
    '''删除队列中url'''
    return __handle.lrem("comebakcallnotify", url)


    
def out_send_num_cbgw(end=50):
    '''待通知队列取一条'''
    # return __handle.lpop("callnotify")
    return __handle.lrange("send_num_cbgw", 0, end)


def in_send_num_cbgw(url):
    '''插入待通知队列'''
    return __handle.rpush("send_num_cbgw", url)

def rm_send_num_cbgw(url):
    '''删除队列中url'''
    return __handle.lrem("send_num_cbgw", url)

def rm_send_num_cbgw_by_position(start=0, end=50):
    '''待通知队列取一条'''
#     __handle = redis.Redis(host=config.REDIS_DB, port=config.REDIS_PORT, password=config.REDIS_PWD, db=config.REDIS_DB)
#     __handle = redis.Redis(host='127.0.0.1', port=6379, db=1)
    # return __handle.lpop("comebakcallnotify")
    return __handle.ltrim('send_num_cbgw', start, end)
def incr_send():
    return __handle.incr("send_count")

def incr_recv():
    return __handle.incr("recv_count")

def incr_relind():
    return __handle.incr("relind_count")

def del_count():
    __handle.delete("send_count")
    __handle.delete("recv_count")
    __handle.delete("relind_count")
    return
